#coding=utf-8
#Boa:Dialog:DlgExpense

import wx

from product import *
import kit

def create(parent):
    return DlgExpense(parent)

[wxID_DLGEXPENSE, wxID_DLGEXPENSEBTNCANCEL, wxID_DLGEXPENSEBTNSAVE, 
 wxID_DLGEXPENSECBACCOUNT, wxID_DLGEXPENSECBSUBJECT, wxID_DLGEXPENSEDATEDATE, 
 wxID_DLGEXPENSESTATICTEXT1, wxID_DLGEXPENSESTATICTEXT2, 
 wxID_DLGEXPENSESTATICTEXT3, wxID_DLGEXPENSESTATICTEXT4, 
 wxID_DLGEXPENSESTATICTEXT5, wxID_DLGEXPENSETCAMOUNT, wxID_DLGEXPENSETCMEMO, 
] = [wx.NewId() for _init_ctrls in range(13)]

class DlgExpense(wx.Dialog):
    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Dialog.__init__(self, id=wxID_DLGEXPENSE, name=u'DlgExpense',
              parent=prnt, pos=wx.Point(390, 230), size=wx.Size(606, 365),
              style=wx.DEFAULT_DIALOG_STYLE, title=u'\u6d88\u8d39\u5f00\u652f')
        self.SetClientSize(wx.Size(598, 331))
        self.SetFont(wx.Font(12, wx.SWISS, wx.NORMAL, wx.BOLD, False, u'Arial'))
        self.SetBackgroundColour(wx.Colour(183, 209, 173))
        self.Center(wx.BOTH)

        self.staticText1 = wx.StaticText(id=wxID_DLGEXPENSESTATICTEXT1,
              label=u'\u4f1a\u8ba1\u79d1\u76ee:', name='staticText1',
              parent=self, pos=wx.Point(40, 80), size=wx.Size(74, 19), style=0)

        self.cbSubject = wx.ComboBox(choices=[], id=wxID_DLGEXPENSECBSUBJECT,
              name=u'cbSubject', parent=self, pos=wx.Point(136, 72),
              size=wx.Size(145, 27), style=0, value=u'')
        self.cbSubject.SetLabel(u'')

        self.staticText2 = wx.StaticText(id=wxID_DLGEXPENSESTATICTEXT2,
              label=u'\u652f\u51fa\u91d1\u989d:', name='staticText2',
              parent=self, pos=wx.Point(40, 160), size=wx.Size(74, 19),
              style=0)

        self.tcAmount = wx.TextCtrl(id=wxID_DLGEXPENSETCAMOUNT,
              name=u'tcAmount', parent=self, pos=wx.Point(136, 152),
              size=wx.Size(100, 28), style=0, value=u'0')

        self.staticText3 = wx.StaticText(id=wxID_DLGEXPENSESTATICTEXT3,
              label=u'\u5907\u6ce8:', name='staticText3', parent=self,
              pos=wx.Point(72, 200), size=wx.Size(40, 19), style=0)

        self.tcMemo = wx.TextCtrl(id=wxID_DLGEXPENSETCMEMO, name=u'tcMemo',
              parent=self, pos=wx.Point(136, 192), size=wx.Size(384, 28),
              style=0, value=u'')

        self.btnSave = wx.Button(id=wxID_DLGEXPENSEBTNSAVE,
              label=u'\u4fdd\u5b58', name=u'btnSave', parent=self,
              pos=wx.Point(176, 256), size=wx.Size(86, 29), style=0)
        self.btnSave.Bind(wx.EVT_BUTTON, self.OnBtnSaveButton,
              id=wxID_DLGEXPENSEBTNSAVE)

        self.btnCancel = wx.Button(id=wxID_DLGEXPENSEBTNCANCEL,
              label=u'\u53d6\u6d88', name=u'btnCancel', parent=self,
              pos=wx.Point(336, 256), size=wx.Size(86, 29), style=0)
        self.btnCancel.Bind(wx.EVT_BUTTON, self.OnBtnCancelButton,
              id=wxID_DLGEXPENSEBTNCANCEL)

        self.dateDate = wx.DatePickerCtrl(id=wxID_DLGEXPENSEDATEDATE,
              name=u'dateDate', parent=self, pos=wx.Point(136, 32),
              size=wx.Size(122, 27), style=wx.DP_DROPDOWN | wx.DP_SHOWCENTURY)

        self.staticText4 = wx.StaticText(id=wxID_DLGEXPENSESTATICTEXT4,
              label=u'\u652f\u51fa\u65e5\u671f:', name='staticText4',
              parent=self, pos=wx.Point(40, 40), size=wx.Size(74, 19), style=0)

        self.staticText5 = wx.StaticText(id=wxID_DLGEXPENSESTATICTEXT5,
              label=u'\u652f\u51fa\u5e10\u6237:', name='staticText5',
              parent=self, pos=wx.Point(40, 120), size=wx.Size(74, 19),
              style=0)

        self.cbAccount = wx.ComboBox(choices=[], id=wxID_DLGEXPENSECBACCOUNT,
              name=u'cbAccount', parent=self, pos=wx.Point(136, 112),
              size=wx.Size(145, 27), style=wx.CB_READONLY, value=u'')
        self.cbAccount.SetLabel(u'')

    def __init__(self, parent):
        self._init_ctrls(parent)
        
        self.myInit()
        
    def myInit(self):
        
        self.curObj = None
        
        self.dateDate.SetValue(wx.DateTime.Today())
        
        self.subject = {}
        for id,str in CDataAccountSubject.fetchSubjectList(income=0):
            self.subject[id] = str
            self.cbSubject.Append(str, id)
        
        self.account = {}
        for id,account in CDataAccount.fetchFields(fields=['id','briefName']):
            self.account[id] = account
            self.cbAccount.Append(account, id)
        
        #print self.subject
    
    def setEditData(self, obj):
        self.curObj = obj
        self.cbSubject.SetStringSelection(self.subject[int(obj.subject)])
        self.tcAmount.SetValue(str(obj.amount))
        self.tcMemo.SetValue(obj.memo)
        self.dateDate.SetValue(wx.DateTimeFromDMY(obj.date.day, obj.date.month-1, obj.date.year, 0,0,0))
        self.dateDate.Enable(False)
        
        self.cbAccount.SetStringSelection(self.account[int(obj.account_id)])
        
    def OnBtnSaveButton(self, event):
        
        if not self.cbSubject.GetValue():
            dlg = wx.MessageDialog(self, u'请先选择会计科目',u'提示',wx.OK|wx.ICON_INFORMATION)
            dlg.ShowModal()
            return
        
        if not self.cbAccount.GetValue():
            dlg = wx.MessageDialog(self, u'请先选择支出账户',u'提示',wx.OK|wx.ICON_INFORMATION)
            dlg.ShowModal()
            return
        
        try:
            amount = float(self.tcAmount.GetValue())
        except:
            dlg = wx.MessageDialog(self, u'请检查金额输入是否正确',u'提示',wx.OK|wx.ICON_INFORMATION)
            dlg.ShowModal()
            return
                
        subject = self.cbSubject.GetClientData(self.cbSubject.GetSelection())
        memo = self.tcMemo.GetValue()
        
        obj = self.curObj if self.curObj else CDataExpense()
        obj.amount = amount
        obj.subject = subject
        obj.memo = memo
        obj.date = kit.str2Date(self.dateDate.GetValue().Format('%Y-%m-%d'))
        obj.account_id = self.cbAccount.GetClientData(self.cbAccount.GetSelection())
        obj.save()
        
        self.EndModal(wx.ID_OK)

    def OnBtnCancelButton(self, event):
        self.EndModal(wx.ID_CANCEL)
